package com.duowei.diandan.adapter;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.DeskGroup;

import java.util.List;

public class DeskCateAdapter extends RecyclerView.Adapter<DeskCateAdapter.ViewHolder> {

    private List<DeskGroup> mList;
    private int mCheckedPos = 0;

    private OnCateItemClickListener mOnCateItemClickListener;

    public DeskCateAdapter(List<DeskGroup> list, OnCateItemClickListener onCateItemClickListener) {
        mList = list;
        mOnCateItemClickListener = onCateItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_item_desk_cate, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DeskGroup deskGroup = mList.get(holder.getAdapterPosition());

        holder.mTextView.setText(deskGroup.getCsmc());

        if (mCheckedPos == position) {
            holder.mTextView.setChecked(true);
        } else {
            holder.mTextView.setChecked(false);
        }

        holder.mTextView.setOnClickListener(v -> {
            mCheckedPos = holder.getAdapterPosition();
            notifyDataSetChanged();

            if (mOnCateItemClickListener != null) {
                mOnCateItemClickListener.onCateItemClick(deskGroup);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        CheckedTextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (CheckedTextView) itemView;
        }
    }

    public interface OnCateItemClickListener {
        void onCateItemClick(DeskGroup deskGroup);
    }
}
